package com.tenebrous.jestic.service.framwork.meber.organ;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tenebrous.jestic.entity.framwork.meber.author.AuthRole;
import com.tenebrous.jestic.entity.framwork.meber.organ.*;
import com.tenebrous.jestic.mapper.idao.framwork.meber.author.AuthRoleMapper;
import com.tenebrous.jestic.mapper.idao.framwork.meber.organ.*;
import com.tenebrous.jestic.service.SuperService;
import com.rain.ops.sdk.dto.ApiResult;
import com.rain.ops.sdk.utils.UUIDUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class OrgUserService extends SuperService<OrgUser> {

    @Autowired
    private OrgUserMapper mapper;

    @Autowired
    private OrgUserAbnomalMapper abbnomalMapper;

    @Autowired
    private OrgUserExtMapper extmapper;

    @Autowired
    private AuthRoleMapper roleMapper;

    @Autowired
    private OrgUserAccountMapper accountMapper;

    @Autowired
    private OrgUserTrainMapper trainMapper;

    @Autowired
    private OrgUserAwardMapper awardMapper;

    @Autowired
    private OrgUserEducateMapper educateMapper;

    @Autowired
    private OrgUserExameMapper exameMapper;

    @Autowired
    private OrgUserFamilyMapper familyMapper;

    @Autowired
    private OrgUserWorkMapper workMapper;

    @Autowired
    private OrgUserCertiMapper certiMapper;

    @Autowired
    private OrgUserWorkMapper orgUserWorkMapper;

    @Autowired
    private OrgOrganizeService organizeService;

    @Autowired
    private OrgUserProcessMapper orgUserProcessMapper;

    @Override
    @Autowired
    protected void setSuperMapper() {
        super.setSuperMapper(this.mapper);
    }

    /**
     * 验证账号
     *
     * @param username
     * @return
     */
    public ApiResult verify(String username) {
        OrgUserAccount account = this.mapper.selectAccountByUsername(username);
        if (account != null) {
            if (Integer.valueOf(1).equals(account.getEnabled())) {
                OrgUser user = this.getById(account.getUserid());
                if (user != null) {
                    user.setUsername(account.getUsername());
                    return new ApiResult(200, "验证成功。", user);
                } else {
                    return new ApiResult(999, "验证失败，用户信息不存在。");
                }
            } else {
                return new ApiResult(999, "验证失败，账号被禁用。");
            }
        }
        return new ApiResult(999, "验证失败，账号不存在。");
    }

    /**
     * 账号验证和密码
     *
     * @param username 账号
     * @param password 密码
     * @return
     */
    public ApiResult verify(String username, String password) {
        OrgUserAccount account = this.mapper.selectAccountByUsername(username);
        if (account != null) {
            if (password.equals(account.getPassword())) {
                if (Integer.valueOf(1).equals(account.getEnabled())) {
                    OrgUser user = this.getById(account.getUserid());
                    if (user != null) {
                        user.setUsername(account.getUsername());
                        return new ApiResult(200, "验证成功。", user);
                    } else {
                        return new ApiResult(999, "验证失败，用户信息不存在。");
                    }
                } else {
                    return new ApiResult(999, "验证失败，账号被禁用。");
                }
            } else {
                return new ApiResult(999, "验证失败，密码错误。");
            }
        }
        return new ApiResult(999, "验证失败，账号不存在。");
    }

    /**
     * 获取指定ID用户
     *
     * @param id 用户ID
     * @return
     */
    public OrgUser getById(String id) {
        OrgUser ouser = this.mapper.selectById(id);
        if (ouser != null) {
            List<AuthRole> rlist = this.roleMapper.selectRoleListByUserid(id, null);
            List<String> roleids = rlist.stream().map(AuthRole::getId).collect(Collectors.toList());
            List<String> rolecodes = rlist.stream().map(AuthRole::getCode).collect(Collectors.toList());
            List<String> rolenames = rlist.stream().map(AuthRole::getName).collect(Collectors.toList());
            List<String> defultIndexs = rlist.stream().map(AuthRole::getDefultIndex).collect(Collectors.toList());

            ouser.setRoleids(StringUtils.join(roleids, ","));
            ouser.setRolecodes(StringUtils.join(rolecodes, ","));
            ouser.setRolenames(StringUtils.join(rolenames, ","));
            ouser.setDefultIndexs(StringUtils.join(defultIndexs, ","));
        }
        return ouser;
    }

    /**
     * 查找用户登陆账号
     *
     * @param userid
     * @return
     */
    public OrgUserAccount getAccountById(String userid) {
        return this.accountMapper.selectByPrimaryKey(userid);
    }

    /**
     * 获取指定用户名用户信息
     *
     * @param username
     * @return
     */
    public OrgUser getByUsername(String username) {
        return this.mapper.selectByUsername(username);
    }

    public List<OrgUser> queryList(String companyid, String departmentid, String etype, String estate, Integer dstate, String realname) {
        return this.queryList(companyid, departmentid, etype, estate, dstate, null, null, realname);
    }

    public List<OrgUser> queryList(String companyid, String departmentid, String etype, String estate, Integer dstate, String poloticalCode, String realname) {
        return this.queryList(companyid, departmentid, etype, estate, dstate, poloticalCode, null, realname);
    }

    public List<OrgUser> queryList(String companyid, String departmentid, String etype, String estate, Integer dstate, String poloticalCode, String edutionCode, String realname) {
        return this.mapper.selectByCondition(companyid, departmentid, etype, estate, dstate, poloticalCode, edutionCode, null, null, null, realname);
    }

    public PageInfo<OrgUser> queryPagingList(Integer pageIndex, Integer pageSize, String companyid, String departmentid, String realname) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> this.mapper.selectByCondition(companyid, departmentid,
                null, null, null, null, null, null, null, null, realname));
    }

    public PageInfo<OrgUser> queryPagingList(Integer pageIndex, Integer pageSize, String companyid, String departmentid, String etype, String estate, Integer dstate, String realname) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> this.mapper.selectByCondition(companyid, departmentid, etype, estate, dstate,
                null, null, null, null, null, realname));
    }

    public PageInfo<OrgUser> queryPagingList(Integer pageIndex, Integer pageSize, String companyid, String departmentid, String etype, String estate, Integer dstate, String politicalCode, String edutionCode, String degreeCode, String sexCode, String marryCode, String realname) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> this.mapper.selectByCondition(companyid, departmentid, etype, estate, dstate, politicalCode, edutionCode, degreeCode, sexCode, marryCode, realname));
    }

    public List<Map<String, Object>> queryStateCount(String companyid, String departmentid) {
        return this.mapper.selectStateCount(companyid, departmentid);
    }

    public List<Map<String, Object>> queryEstateCount(String companyid, String departmentid) {
        return this.mapper.selectEstateCount(companyid, departmentid);
    }

    public List<Map<String, Object>> queryEtypeCount(String companyid, String departmentid) {
        return this.mapper.selectEtypeCount(companyid, departmentid);
    }

    /**
     * 更新用户信息(用户信息初始化)
     *
     * @param userInfo
     */
//    @Transactional
//    public ApiResult updateprocinstid(OrgUser userInfo, OrgUser ouser) {
//        //userInfo.setDstate(0);
//        ApiResult api = updateResumeInfo(userInfo, ouser);
//        //保存的时候改变流程实例的主题
//        if (StringUtils.isNotBlank(userInfo.getProcinstid())) {
//            String title = ("【" + userInfo.getRealname() + "】" + "信息初始化");
//            runtimeService.updateProcessTitle(userInfo.getProcinstid(), title);
//        }
//        return api;
//    }

//    @Transactional
//    public ApiResult saveOrgUserProcess(String usejson, String procinstid, String uid, String stype) {
//        OrgUserProcess item = orgUserProcessMapper.selectByPrimaryKey(procinstid);
//        OrgUser user = mapper.selectById(uid);
//        if (item == null) {
//            item = new OrgUserProcess();
//            item.setId(procinstid);
//            item.setUserid(uid);
//            item.setProcinstid(procinstid);
//            item.setAuserjson(usejson);
//            orgUserProcessMapper.insert(item);
//        } else {
//            item.setAuserjson(usejson);
//            orgUserProcessMapper.updateByPrimaryKeySelective(item);
//        }
//        //保存的时候改变流程实例的主题
//        if (StringUtils.isNotBlank(procinstid)) {
//            String title = ("【" + user.getRealname() + "】" + stype);
//            runtimeService.updateProcessTitle(procinstid, title);
//        }
//        return new ApiResult(200, "保存成功");
//    }

    /**
     * 更新履历表
     *
     * @param userInfo
     */
    @Transactional
    public ApiResult updateResumeInfo(OrgUser userInfo, OrgUser ouser) {
        Date dt = new Date();
        if (StringUtils.isNotBlank(userInfo.getId())) {
            userInfo.setUpdateDate(dt);
            userInfo.setUpdateUserid(ouser != null ? ouser.getId() : null);
//            this.mapper.updateByPrimaryKeySelective(userInfo);

            this.workMapper.updateForResumeByUserid(userInfo.getId(), 0);
            this.awardMapper.updateForResumeByUserid(userInfo.getId(), 0);
            this.trainMapper.updateForResumeByUserid(userInfo.getId(), 0);
            this.certiMapper.updateForResumeByUserid(userInfo.getId(), 0);
            this.familyMapper.updateForResumeByUserid(userInfo.getId(), 0);
            this.educateMapper.updateForResumeByUserid(userInfo.getId(), 0);
            this.exameMapper.updateForResumeByUserid(userInfo.getId(), 0);
//            this.languageMapper.updateForResumeByUserid(userInfo.getId(), 0);
//            this.achievmentMapper.updateForResumeByUserid(userInfo.getId(), 0);
//            this.presentMapper.updateForResumeByUserid(userInfo.getId(), 0);
//            this.tectitleMapper.updateForResumeByUserid(userInfo.getId(), 0);
        } else {
            userInfo.setCreateUserid(ouser != null ? ouser.getId() : null);
            userInfo.setCreateDate(dt);
            userInfo.setId(UUIDUtils.randomUUID());
            this.mapper.insertSelective(userInfo);
        }
        /**
         * 身份证、征信报告附件
         */
        OrgUserExt uext = new OrgUserExt();
        if (this.extmapper.existsWithPrimaryKey(uext.getId())) {
            this.extmapper.updateByPrimaryKeySelective(uext);
        } else {
            this.extmapper.insertSelective(uext);
        }
        this.mapper.updateByPrimaryKeySelective(userInfo);
        return new ApiResult(200, "更新成功。", userInfo);
    }

    /**
     * 更新用户头像
     *
     * @param userid
     * @param avatar base64编码
     */
    public ApiResult updatetAvatar(String userid, String avatar) {
        OrgUser item = this.mapper.selectByPrimaryKey(userid);
        if (item != null) {
            item.setAvatar(avatar);
            this.updateSelective(item);
            return new ApiResult(200, "更新成功。", item);
        } else {
            return new ApiResult(999, "更新失败，未找到指定用户：" + userid);
        }
    }

    @Transactional
    public int deleteByIds(String[] userids) {
        int i = 0;
        for (String id : userids) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("ext4", id);
            if (StringUtils.isNotBlank(id)) {
                i += this.mapper.deleteByPrimaryKey(id);
                trainMapper.deleteByUid(id);
                awardMapper.deleteByUid(id);
                educateMapper.deleteByUid(id);
                familyMapper.deleteByUserid(id);
                orgUserWorkMapper.deleteByUid(id);
                accountMapper.deleteByUserid(id);
//                storeClient.delete(map);
            }
        }
        return i;
    }

    /**
     * 修改账号密码
     *
     * @param username    登陆账号
     * @param newpassword 新密码，加密后。
     * @param oldpassword 旧密码，加密后。
     * @return
     */
    @Transactional
    public ApiResult updatePassword(String username, String newpassword, String oldpassword) {
        OrgUserAccount account = this.mapper.selectAccountByUsername(username);
        if (account != null) {
            if (oldpassword.equals(account.getPassword())) {
                account.setUpdateDate(new Date());
                account.setPassword(newpassword);
                return new ApiResult(200, "更新成功。", this.accountMapper.updateByPrimaryKeySelective(account));
            } else {
                return new ApiResult(999, "更新错误，旧密码错误。");
            }
        } else {
            return new ApiResult(999, "更新错误，改账号不存在。");
        }
    }

    /**
     * @param item
     * @return
     */
    public int updateAccountSelective(OrgUserAccount item) {
        return this.accountMapper.updateByPrimaryKeySelective(item);
    }

    public int saveAccountSelective(OrgUserAccount item) {
        return this.accountMapper.insertSelective(item);
    }

    @Transactional
    public int initAccountPwd(String[] userids) {
        int i = 0;
        for (String id : userids) {
            OrgUserAccount uc = this.accountMapper.selectByPrimaryKey(id);
            if (uc != null) {
                uc.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
                this.accountMapper.updateByPrimaryKeySelective(uc);
                i++;
            }
        }
        return i;
    }

    /**
     * 调整员工单位/部门
     *
     * @param userid
     * @param targetDepartmentid
     * @return
     */
    @Transactional
    public ApiResult unusualOrgan(String userid, String targetDepartmentid, OrgUser ouser) {
        Date dt = new Date();
        OrgUser user = this.mapper.selectByPrimaryKey(userid);
        if (user != null) {
            if (!user.getDepartmentid().equals(targetDepartmentid)) {
                OrgOrganize tdepartment = this.organizeService.selectByPrimaryKey(targetDepartmentid);
                if (tdepartment != null) {
                    String before = JSON.toJSONString(user);
                    List<String> spath = new ArrayList<>();
                    List<String> tpath = new ArrayList<>();
                    organizeService.getPrePath(tpath, tdepartment.getId());
                    organizeService.getPrePath(spath, user.getDepartmentid());

                    user.setUpdateDate(dt);
                    user.setDepartmentid(tdepartment.getId());
                    user.setUpdateUserid(ouser != null ? ouser.getId() : null);
                    this.mapper.updateByPrimaryKeySelective(user);
                    String after = JSON.toJSONString(user);

                    OrgUserAbnomal abnomal = new OrgUserAbnomal();
                    abnomal.setId(UUIDUtils.randomUUID());
                    abnomal.setUserid(user.getId());
                    abnomal.setSevent("调组织");
                    abnomal.setCreateDate(dt);
                    abnomal.setAbnomalAf(after);
                    abnomal.setAbnomalBf(before);
                    abnomal.setCreateUserid(ouser != null ? ouser.getId() : null);
                    abnomal.setRemark(String.format("调整员工所在组织：【%s】调整为【%s】", StringUtils.join(spath, "/"), StringUtils.join(tpath, "/")));

                    this.abbnomalMapper.insertSelective(abnomal);

                    return new ApiResult(200, "更新成功。");
                } else {
                    return new ApiResult(999, "更新失败，目标组织不存在。");
                }
            } else {
                return new ApiResult(200, "更新成功，但未发生任何变化。");
            }
        }
        return new ApiResult(999, "更新失败。");
    }

    /**
     * 调整刚序/岗位
     *
     * @param userid
     * @param targetStationid
     * @param targetStationgradid
     * @param ouser
     * @return
     */
    @Transactional
    public ApiResult unusualStation(String userid, String targetStationid, String targetStationgradid, OrgUser ouser) {
        Date dt = new Date();
        OrgUser user = this.mapper.selectById(userid);
        if (user != null) {
            String before = JSON.toJSONString(user);
            user.setUpdateDate(dt);
            user.setStationid(targetStationid);
            user.setStationgradid(targetStationgradid);
            user.setUpdateUserid(ouser != null ? ouser.getId() : null);
            this.mapper.updateByPrimaryKeySelective(user);
            String after = JSON.toJSONString(user);

            OrgUser user2 = this.mapper.selectById(userid);
            OrgUserAbnomal abnomal = new OrgUserAbnomal();
            abnomal.setId(UUIDUtils.randomUUID());
            abnomal.setUserid(user.getId());
            abnomal.setSevent("调岗位");
            abnomal.setCreateDate(dt);
            abnomal.setAbnomalAf(after);
            abnomal.setAbnomalBf(before);
            abnomal.setCreateUserid(ouser != null ? ouser.getId() : null);
            abnomal.setRemark(String.format("调整员工岗位：【%s】调整为【%s】", user.getStationtext(), user2.getStationtext()));
            this.abbnomalMapper.insertSelective(abnomal);
            return new ApiResult(200, "更新成功。");
        }
        return new ApiResult(999, "更新失败。");
    }

    /**
     * 调整员工类型
     *
     * @param userid
     * @param targetEtype
     * @return
     */
    @Transactional
    public ApiResult unusualEtype(String userid, String targetEtype, OrgUser ouser) {
        Date dt = new Date();
        OrgUser user = this.mapper.selectByPrimaryKey(userid);
        if (user != null) {
            if (!targetEtype.equals(user.getEtype())) {
                String beforeEtype = user.getEtype();
                String sbefore = JSON.toJSONString(user);
                user.setUpdateDate(dt);
                user.setEtype(targetEtype);
                user.setUpdateUserid(ouser != null ? ouser.getId() : null);
                this.mapper.updateByPrimaryKeySelective(user);
                String safter = JSON.toJSONString(user);

                OrgUserAbnomal abnomal = new OrgUserAbnomal();
                abnomal.setId(UUIDUtils.randomUUID());
                abnomal.setUserid(user.getId());
                abnomal.setSevent("调类型");
                abnomal.setAbnomalBf(sbefore);
                abnomal.setAbnomalAf(safter);
                abnomal.setRemark(String.format("调整员工类型：【%s】调整为【%s】", beforeEtype, targetEtype));
                abnomal.setCreateUserid(ouser != null ? ouser.getId() : "");
                abnomal.setCreateDate(dt);
                this.abbnomalMapper.insertSelective(abnomal);

                return new ApiResult(200, "更新成功。");
            } else {
                return new ApiResult(200, "更新成功，但未发生任何变化。");
            }
        }
        return new ApiResult(999, "更新失败。");
    }

    /**
     * 调整员工状态
     *
     * @param userid
     * @param targetEstate
     * @return
     */
    @Transactional
    public ApiResult unusualEstate(String userid, String targetEstate, OrgUser ouser) {
        Date dt = new Date();
        OrgUser user = this.mapper.selectByPrimaryKey(userid);
        if (user != null) {
            if (!targetEstate.equals(user.getEstate())) {
                String beforeEstate = user.getEstate();
                String sbefore = JSON.toJSONString(user);
                user.setUpdateDate(dt);
                user.setEstate(targetEstate);
                user.setUpdateUserid(ouser != null ? ouser.getId() : null);
                this.mapper.updateByPrimaryKeySelective(user);
                String safter = JSON.toJSONString(user);

                OrgUserAbnomal abnomal = new OrgUserAbnomal();
                abnomal.setId(UUIDUtils.randomUUID());
                abnomal.setUserid(user.getId());
                abnomal.setSevent("调状态");
                abnomal.setAbnomalBf(sbefore);
                abnomal.setAbnomalAf(safter);
                abnomal.setRemark(String.format("调整员工状态：【%s】调整为【%s】", beforeEstate, targetEstate));
                abnomal.setCreateUserid(ouser != null ? ouser.getId() : "");
                abnomal.setCreateDate(dt);
                this.abbnomalMapper.insertSelective(abnomal);

                return new ApiResult(200, "更新成功。");
            } else {
                return new ApiResult(200, "更新成功，但未发生任何变化。");
            }
        }
        return new ApiResult(999, "更新失败。");
    }

    public List<Map<String, Object>> analysEdution(String companyid) {
        return this.mapper.selectAnalysEdution(companyid);
    }

    public List<Map<String, Object>> analysEdutionCompany(String name) {
        return this.mapper.selectAnalysEdutionCompany(name);
    }

    public List<Map<String, Object>> analysEdutionDepartment(String companyid, String name) {
        return this.mapper.selectAnalysEdutionDepartment(companyid, name);
    }

    public List<Map<String, Object>> analysAge(String companyid) {
        return this.mapper.selectAnalysAge(companyid);
    }

    public List<Map<String, Object>> analysAgeCompany(String agerange) {
        return this.mapper.selectAnalysAgeCompany(agerange);
    }

    public List<Map<String, Object>> analysAgeDepartment(String companyid, String agerange) {
        return this.mapper.selectAnalysAgeDepartment(companyid, agerange);
    }

    public List<OrgUser> analysAgeDepartmentUsers(String departmentid, String agerange) {
        return this.mapper.selectAnalysAgeDepartmentUsers(departmentid, agerange);
    }

    public List<Map<String, Object>> analysSex(String companyid) {
        return this.mapper.selectAnalysSex(companyid);
    }

    public List<Map<String, Object>> analysSexCompany(String sex) {
        return this.mapper.selectAnalysSexCompany(sex);
    }

    public List<Map<String, Object>> analysSexDepartment(String companyid, String sex) {
        return this.mapper.selectAnalysSexDepartment(companyid, sex);
    }

    public List<OrgUser> analysSexDepartmentUsers(String departmentid, String sex) {
        return this.mapper.selectAnalysSexDepartmentUsers(departmentid, sex);
    }

    public List<Map<String, Object>> analysCompany() {
        return this.mapper.selectAnalysCompany();
    }

    public List<Map<String, Object>> analysCompanyDepartment(String companyid) {
        return this.mapper.selectAnalysCompanyDepartment(companyid);
    }

    public List<OrgUser> analysCompanyDepartmentUsers(String departmentid) {
        return this.mapper.selectAnalysCompanyDepartmentUsers(departmentid);
    }

    @Transactional
    public ApiResult saveUserAndAccount(OrgUser item, OrgUser user) {
        Date dt = new Date();
        if (StringUtils.isNotBlank(item.getUsername())) {//账号是否存在
            List<OrgUserAccount> list = accountMapper.queryByUserName(item.getUsername());
            if (list.size() > 0) {
                return new ApiResult(999, "此账号已近存在，请重新设置账号!");
            } else {
                String userid = UUIDUtils.randomUUID();
                item.setId(userid);
                item.setDstate(1);
                item.setEstate("在职");
                item.setCreateDate(dt);
                mapper.insertSelective(item);

                OrgUserAccount it = new OrgUserAccount();
                it.setId(userid);
                it.setUserid(userid);
                it.setUsername(item.getUsername());
                it.setEnabled(1);
                it.setCreateDate(dt);
                it.setCreateUserid(user.getId());
                accountMapper.insertSelective(it);
            }
        }
        return new ApiResult(200, "账号新建成功，已通过短信通知此人。");
    }

    @Transactional
    public ApiResult updateUserAndAccount(OrgUser item, OrgUser user) {
        item.setUpdateDate(new Date());
        item.setUpdateUserid(user.getId());
        mapper.updateByPrimaryKey(item);

        OrgUserAccount it = accountMapper.selectByPrimaryKey(item.getId());
        it.setUsername(item.getUsername());
        it.setUpdateDate(new Date());
        it.setUpdateUserid(user.getId());
        accountMapper.updateByPrimaryKey(it);
        return new ApiResult(200, "账号修改成功，已通过短信通知此人。");
    }

    public PageInfo<OrgUser> pagingMailList(int pageIndex, int pageSize, String realname, String mobile, String companyid, String departmentid) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> this.mapper.selectMailByCondition(realname, mobile, companyid, departmentid));
    }

    public List<OrgUserAbnomal> queryAbnomalList(String userid) {
        return this.abbnomalMapper.selectByUserid(userid);
    }

    public List<OrgUser> queryList(String[] userids) {
        return this.mapper.selectByUserids(userids);
    }

    public OrgUser selectByMobile(String mobile) {
        return this.mapper.selectByMobile(mobile);
    }

//    @Autowired
//    private TmsMsgUserMappingMapper tmsMsgUserMappingMapper;
//
//    public List<OrgUserVo> getUserList(String noticeId) {
//        List<OrgUserVo> orgUsers = this.mapper.selectByUserList(noticeId);
//        List<OrgUserVo> list = orgUsers.stream().filter(x -> StringUtils.isNotBlank(x.getMobile())).collect(Collectors.toList());
//        for (OrgUserVo vo : list) {
//            vo.setMsgId(noticeId);
//            TmsMsgUserMapping mapping = new TmsMsgUserMapping();
//            mapping.setUserId(vo.getId());
//            mapping.setMsgNoticeId(noticeId);
//            TmsMsgUserMapping userMapping = tmsMsgUserMappingMapper.selectOne(mapping);
//            if (Objects.isNull(userMapping)) {
//                vo.setIsNotice(0);
//            }else {
//                vo.setIsNotice(1);
//            }
//        }
//        return list;
//    }

    public ApiResult verifyMobile(String mobile) {
        OrgUser orgUser = this.selectByMobile(mobile);
        OrgUserAccount accountQueryWrapper = new OrgUserAccount();
        accountQueryWrapper.setUserid(orgUser.getId());
        OrgUserAccount account = this.accountMapper.selectOne(accountQueryWrapper);
        if (account != null) {
                if (Integer.valueOf(1).equals(account.getEnabled())) {
                    OrgUser user = this.getById(account.getUserid());
                    if (user != null) {
                        user.setUsername(account.getUsername());
                        return new ApiResult(200, "验证成功。", user);
                    } else {
                        return new ApiResult(999, "验证失败，用户信息不存在。");
                    }
                } else {
                    return new ApiResult(999, "验证失败，账号被禁用。");
                }
            }

        return new ApiResult(999, "验证失败，账号不存在。");
    }
}
